﻿#include "plansform.h"
#include "ui_plansform.h"

#include <QFile>
#include <QGraphicsDropShadowEffect>
#include <QMessageBox>
#include <QTextStream>

PlansForm::PlansForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PlansForm)
{
    ui->setupUi(this);
    m_sql.init("PlanForm_data");
    InitPage();


}

PlansForm::~PlansForm()
{
    delete ui;
}

void PlansForm::InitPage()
{
    ui->tableWidget->setShowGrid(true);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    //自动调整窗口列表的【宽、高】---适应窗口大小
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->tableWidget->verticalHeader()->hide();//取消默认行号
    QStringList header;
    header << tr("Index")<< tr("plan_name")<< tr("wood_type")<< tr("scan_mode") << tr("defect_config")<< tr("createtime")<< tr("changetime");
    ui->tableWidget->setColumnCount(header.size());
    ui->tableWidget->setHorizontalHeaderLabels(header);

    m_nCurPage = 1;
    m_nRowsPerPage = 7;
    m_nTotalPages = 1;

    ui->Bt_Change->setEnabled(false);
    ui->Bt_Delete->setEnabled(false);
    ui->Bt_Search->setEnabled(false);

    UpdateTable();
    connect(ui->tableWidget->verticalScrollBar(), SIGNAL(valueChanged(int)),this,SLOT(OnSlideAction(int)));//响应滚动条翻页

    ui->Bt_Return->hide();


    QGraphicsDropShadowEffect *shadow_effectA = new QGraphicsDropShadowEffect(this);
    QGraphicsDropShadowEffect *shadow_effectB = new QGraphicsDropShadowEffect(this);

    shadow_effectA->setOffset(0, 0);
    shadow_effectA->setColor(QColor(150,150,150));
    shadow_effectA->setBlurRadius(6);

    shadow_effectB->setOffset(0, 0);
    shadow_effectB->setColor(QColor(150,150,150));
    shadow_effectB->setBlurRadius(6);


    ui->plan_gBox_1->setGraphicsEffect(shadow_effectA);
    ui->plan_gBox_2->setGraphicsEffect(shadow_effectB);

    //允许绘制背景色交替 必须写这句否则交替背景色没有效果
    ui->tableWidget->setAlternatingRowColors(true);
}

void PlansForm::UpdateTable()
{
    QList<T_PlanLine>  recoders = m_sql.queryAllPlanLines();

    ui->tableWidget->clearContents();
    if(recoders.size()%m_nRowsPerPage ==0){
        m_nTotalPages = recoders.size()/m_nRowsPerPage;
    }else{
        m_nTotalPages = recoders.size()/m_nRowsPerPage+1;
    }
    ui->labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));
    ui->tableWidget->setRowCount(recoders.size()); // 设置显示的行数
    for(int i=0;i<recoders.size();i++){
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(recoders[i].id)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(recoders[i].planName));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(recoders[i].woodType));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(recoders[i].scanName));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(recoders[i].schemeName));
        ui->tableWidget->setItem(i,5,new QTableWidgetItem(recoders[i].createTime));
        ui->tableWidget->setItem(i,6,new QTableWidgetItem(recoders[i].changeTime));
        for(int j=0;j<7;j++){
            ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        }
    }

}

void PlansForm::slotsDlgInfo()
{
    UpdateTable();
}

void PlansForm::on_Bt_PrePage_clicked()
{
    m_nCurPage--;
    if(m_nCurPage <=0){
        m_nCurPage = m_nTotalPages;
    }
    ui->tableWidget->verticalScrollBar()->setSliderPosition(m_nCurPage*m_nRowsPerPage - m_nRowsPerPage);
    ui->labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));
}

void PlansForm::on_Bt_NextPage_clicked()
{
    m_nCurPage++;
    if (m_nCurPage > m_nTotalPages)
    {
        m_nCurPage = 1;
    }

    ui->tableWidget->verticalScrollBar()->setSliderPosition(m_nCurPage*m_nRowsPerPage - m_nRowsPerPage);
    ui->labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));

}

void PlansForm::OnSlideAction(int nRowIndex)
{

    ++nRowIndex;
    //计算当前行是那一页
    if (nRowIndex % m_nRowsPerPage == 0)
    {
        m_nCurPage = nRowIndex / m_nRowsPerPage;
    }
    else
    {
        m_nCurPage = nRowIndex / m_nRowsPerPage+1;
    }
    ui->labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));

}

void PlansForm::on_Bt_Add_clicked()
{
    PlanDlg* m_dAdd = new PlanDlg();
    m_dAdd->iniPage(1);
    m_dAdd->show();
    connect(m_dAdd,&PlanDlg::sigFormInfo,this,&PlansForm::slotsDlgInfo);
}

void PlansForm::on_Bt_Change_clicked()
{
    PlanDlg* m_dChange = new PlanDlg();
    m_dChange->iniPage(3);
    m_dChange->show();
    m_dChange->showTabel(m_strCurID.toInt());
    connect(m_dChange, &PlanDlg::sigFormInfo, this, &PlansForm::slotsDlgInfo);
}

void PlansForm::on_Bt_Search_clicked()
{
    PlanDlg* m_dSearch = new PlanDlg();
    m_dSearch->iniPage(2);
    m_dSearch->show();
    m_dSearch->showTabel(m_strCurID.toInt());
}

void PlansForm::on_Bt_Delete_clicked()
{
    if (!(QMessageBox::question(this, tr("Delete"), tr("Whether to delete the current row?"), tr("Yes"), tr("No"))))
    {
        m_sql.deleteSingleData(m_strCurID.toInt(),"t_plan_lines");
        UpdateTable();
    }
}

void PlansForm::on_tableWidget_cellClicked(int row, int column)
{
    column = 0;
    if(m_strCurID.isEmpty()){
        ui->Bt_Change->setEnabled(true);
        ui->Bt_Delete->setEnabled(true);
        ui->Bt_Search->setEnabled(true);
    }
    QTableWidgetItem* item = ui->tableWidget->item(row ,0);
    m_strCurID = item->text();
}



void PlansForm::on_tableWidget_cellDoubleClicked(int row, int column)
{
    column = row;
    on_Bt_Search_clicked();
}
